package com.ifreshwing.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;

import com.ifreshwing.ParkSysApplication;
import com.ifreshwing.activity.base.BaseActivity;
import com.ifreshwing.fragment.ExpireFragment;
import com.ifreshwing.fragment.HomeFragment;
import com.ifreshwing.fragment.IllegalFragment;
import com.ifreshwing.fragment.MineFragment;
import com.ifreshwing.userpark.R;
import com.ifreshwing.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.github.leibnik.wechatradiobar.WechatRadioButton;
import io.github.leibnik.wechatradiobar.WechatRadioGroup;

public class MainActivity extends BaseActivity {

    @BindView(R.id.coorLayout)
    CoordinatorLayout coordinatorLayout;
    private ViewPager viewPager;
    private WechatRadioGroup wechatRadioGroup;
    private WechatRadioButton homeRedioBtn;
    private Fragment homeFragment, expireFragment, illegalFragment,mingFragment ;
    private List<Fragment> list;
    private FragmentPagerAdapter adapter;
    private long exit_time = 0;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ParkSysApplication.getInstance().addActivity(this);
        ButterKnife.bind(this);
        context = this;
        initView();
        initData();
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.activity_home_viewpager);
        wechatRadioGroup = (WechatRadioGroup) findViewById(R.id.dibudaohanglan);
        homeRedioBtn = (WechatRadioButton) findViewById(R.id.rb_home);
        homeFragment = new HomeFragment();
        expireFragment = new ExpireFragment();
        illegalFragment = new IllegalFragment();
        mingFragment = new MineFragment();
        homeRedioBtn.setRadioButtonChecked(true);
        list = new ArrayList<>();
    }

    private void initData() {
        list.add(homeFragment);
        list.add(expireFragment);
        list.add(illegalFragment);
        list.add(mingFragment);
        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        };

        viewPager.setAdapter(adapter);
        wechatRadioGroup.setViewPager(viewPager);
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtil.showSnackBar(coordinatorLayout, "再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                ParkSysApplication.getInstance().exitApp();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
